package com.letao.server.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Bean("ttlExchange")
    public Exchange ttlExchange() {
        Exchange exchange = ExchangeBuilder.topicExchange("ttl_exchange").durable(true).build();
        return exchange;
    }

    @Bean("ttlQueue")
    public Queue ttlQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", "dlx_exchange");
        map.put("x-dead-letter-routing-key", "dlx.order");
        return QueueBuilder.durable("ttl_queue").ttl(15 * 60 * 1000).withArguments(map).build();
    }

    @Bean
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue, @Qualifier("ttlExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl.#").noargs();
    }

    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        Exchange exchange = ExchangeBuilder.topicExchange("dlx_exchange").durable(true).build();
        return exchange;
    }

    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx_queue").build();
    }

    @Bean
    public Binding dlxBinding(@Qualifier("dlxQueue") Queue queue, @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }

}

